package zisu.netty.practice.NIO;

import java.nio.ByteBuffer;

/**
 * @Desc: ""
 * @Author: caixiang
 * @DATE: 2020/7/1 9:49
 */
public class ReadOnlyBuffer {


    public static void main(String[] args) {
        //创建buffer
        ByteBuffer byteBuffer = ByteBuffer.allocate(64);

        for(int i=0;i<64;i++){
            byteBuffer.put((byte)i);
        }

        byteBuffer.flip();

        //得到一个只读 的 buffer
        ByteBuffer readOnlyBuffer = byteBuffer.asReadOnlyBuffer();
        System.out.println(readOnlyBuffer.getClass());

        //读取
        while (readOnlyBuffer.hasRemaining()){
            System.out.println(readOnlyBuffer.get());
        }

        //只读 不能往里面放数据
        readOnlyBuffer.put((byte) 100);  //会报异常ReadOnluBufferException

    }
}
